package com.wangxinyuchuan.demo.iterator;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangxinyuchuan
 * @date 2023/4/16 5:45 PM
 */
public class BookShelf implements Aggregate<Book>{

    private List<Book> bookList;


    public BookShelf(int maxsize) {
        // 这里限制了最大容量，但是appendBook依然不会出错，因为ArrayList会自动扩容
        this.bookList = new ArrayList<>(maxsize);
    }

    public Book getBookAt(int index) {
        return bookList.get(index);
    }

    public int getLength() {
        return bookList.size();
    }

    public void appendBook(Book book) {
        bookList.add(book);
    }

    @Override
    public Iterator<Book> iterator() {
        return new BookShelfIterator(this);
    }
}
